import java.awt.Font;
import java.awt.Rectangle;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;

import javax.swing.JButton;
import javax.swing.JLabel;
import javax.swing.JList;
import javax.swing.JOptionPane;
import javax.swing.JPanel;
import javax.swing.JScrollPane;
import javax.swing.JTextField;

/**
 * 
 * Gui of search review window for the user's use
 */
public class SearchReviewG extends JPanel implements ActionListener
{

	public static final long serialVersionUID = 1L;
	public JLabel MainTitle =null;
	public JScrollPane jScrollTitle = null;
	public JTextField jTitleField = null;
	public JScrollPane jScrollAuthor = null;
	public JTextField jAuthorField = null;
	public JLabel jLabel1 = null;
	public JLabel jLabel2 = null;
	public JLabel jLabel4 = null;
	public JScrollPane jScrollreviewres = null;
	public JLabel jLabelResult = null;
	public JButton jButtonSerachR = null;
	public JButton jBackButton = null;
	public JButton jLogoutButton = null;
	public JList jSearchRList = null;
	public JScrollPane jScrollResAuthor = null;
	public JList jAuthorList = null;
	public JLabel jResAuthorLabel = null;
	public JButton jShowRButton = null;
	public JScrollPane jScrollKeyWords = null;
	public JTextField jKeyWordsField = null;
	/**
	 * This is the default constructor
	 */
	public SearchReviewG() {
		super();
		initialize();
	}

	/**
	 * This method initializes search review window
	 * Lable1 - Title label
	 * Lable2 - Author label
	 * Lable3 - Keywords label
	 * jSearchRList - JList for the book that has a review
	 * @return void
	 */
	public void initialize() 
	{
		jLabelResult = new JLabel();
		jLabelResult.setBounds(new Rectangle(250, 80, 114, 19));
		jLabelResult.setText("Search Result:");
		this.setSize(720, 500);
		this.setLayout(null);
		MainTitle = new JLabel();
		MainTitle.setBounds(new Rectangle(260, 11, 150, 23));
		MainTitle.setText("Search Review");
		MainTitle.setFont(new Font(Font.SANS_SERIF ,Font.ITALIC,20));
		MainTitle.setBounds(200,20,300,50);
		jLabel2 = new JLabel();
		jLabel2.setBounds(new Rectangle(10, 115, 51, 14));
		jLabel2.setText("Author:");
		jLabel1 = new JLabel();
		jLabel1.setBounds(new Rectangle(10, 75, 33, 14));
		jLabel1.setText("Title:");
		jLabel4 = new JLabel();
		jLabel4.setBounds(new Rectangle(10, 155, 67, 14));
		jLabel4.setText("KeyWords:");
		jResAuthorLabel = new JLabel();
		jResAuthorLabel.setBounds(new Rectangle(555, 125, 45, 16));
		jResAuthorLabel.setText("Author:");
		this.add(MainTitle,null);
		this.add(jLabel1,null);
		this.add(jLabel2,null);
		this.add(jLabel4,null);
		this.add(getJScrollTitle(), null);
		this.add(getJScrollAuthor(), null);
		this.add(getJScrollreviewres(), null);
		this.add(jLabelResult, null);
		this.add(getJButtonSerachR(), null);
		this.add(getJBackButton(), null);
		this.add(getJLogoutButton(), null);
		this.add(getJScrollResAuthor(), null);
		this.add(jResAuthorLabel, null);
		this.add(getJShowRButton(), null);
		this.add(getJScrollKeyWords(), null);
	}
	/**
	 * This method initializes jScrollTitle and set Portview on Title TextField	
	 * 	
	 * @return javax.swing.JScrollPane
	 */
	public JScrollPane getJScrollTitle() {
		if (jScrollTitle == null) {
			jScrollTitle = new JScrollPane();
			jScrollTitle.setBounds(new Rectangle(80, 75, 145, 35));
			jScrollTitle.setViewportView(getJTitleField());
		}
		return jScrollTitle;
	}

	/**
	 * This method initializes jTitleField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	public JTextField getJTitleField() {
		if (jTitleField == null) {
			jTitleField = new JTextField();
		}
		return jTitleField;
	}

	/**
	 * This method initializes jScrollAuthor and set Portview on Title Author TextField
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	public JScrollPane getJScrollAuthor() {
		if (jScrollAuthor == null) {
			jScrollAuthor = new JScrollPane();
			jScrollAuthor.setBounds(new Rectangle(80, 115, 145, 35));
			jScrollAuthor.setViewportView(getJAuthorField());
		}
		return jScrollAuthor;
	}

	/**
	 * This method initializes jAuthorField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	public JTextField getJAuthorField() {
		if (jAuthorField == null) {
			jAuthorField = new JTextField();
		}
		return jAuthorField;
	}

	/**
	 * This method initializes jScrollreviewres	and set Portview on search review result JList
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	public JScrollPane getJScrollreviewres() {
		if (jScrollreviewres == null) {
			jScrollreviewres = new JScrollPane();
			jScrollreviewres.setBounds(new Rectangle(250, 115, 300, 250));
			jScrollreviewres.setViewportView(getJSearchRList());
		}
		return jScrollreviewres;
	}

	/**
	 * This method initializes jButtonSerachR and set listener on this button
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJButtonSerachR() {
		if (jButtonSerachR == null) {
			jButtonSerachR = new JButton();
			jButtonSerachR.setBounds(new Rectangle(540, 390, 140, 48));
			jButtonSerachR.setText("Search");
			jButtonSerachR.addActionListener(this);
		}
		return jButtonSerachR;
	}

	@Override
	/**
	 * this method is activated when one of the listeners was triggered
	 */
	public void actionPerformed(ActionEvent ae) 
	{
		if(ae.getSource() == getJLogoutButton())
		{
			if(JOptionPane.showConfirmDialog(null, "Are you sure you want to logout ?","Confirmation", 0) == 0)
			{
				this.setEnabled(false);
				this.setVisible(false);
				Main.log.loginData=new String[3]; //GIL
				Main.log.loginData[0]=Main.gusers.id;
				Main.log.loginData[1]="logout";
				Main.log.loginData[2]=Main.gusers.id;
				Main.log.connection.accept("logout"); //GIL
				Main.log.flag=false;//GIL
				while(Main.log.flag==false);//GIL
				Main.log.flag=false;//GIL
				Main.log.user.setText("");
				Main.log.pass.setText("");
				Main.log.frame.setSize(650,550);
				Main.log.panel.setEnabled(true);
				Main.log.panel.setVisible(true);
			}
		}
		if(ae.getSource()== getJBackButton())
		{
			this.setEnabled(false);
			this.setVisible(false);
			if(Main.gusers.authorozations==1) Main.log.frame.setSize(450,320);
			else if(Main.gusers.authorozations==2) Main.log.frame.setSize(450,400);
			else if(Main.gusers.authorozations==3) Main.log.frame.setSize(450,515);
			Main.welcomeScreen.panel.setEnabled(true);
			Main.welcomeScreen.panel.setVisible(true);	
		}
		if(ae.getSource()== getJButtonSerachR())
		{
			Object[] options = {"Wide Search(OR between values)","Exact Search(AND between values)"};
			int n=JOptionPane.showOptionDialog(null,"Please select Search Type yo want to execute: ","Search Book",JOptionPane.YES_NO_CANCEL_OPTION,JOptionPane.QUESTION_MESSAGE,null,options,options[1]);
			Main.searchReviewC.searchR(n);
		}
		if (ae.getSource() == getJShowRButton())
		{
			if(!(Main.searchReviewC.sRflag))
	    	{
	    		 JOptionPane.showMessageDialog(null,"Please execute Search before!!!","ERROR",JOptionPane.ERROR_MESSAGE);
	    		 return;
	    	}
			else if (Main.searchReviewG.jSearchRList.getSelectedValue() == null)
				 {
						JOptionPane.showMessageDialog(null,"Please choose a book ","ERROR",JOptionPane.ERROR_MESSAGE);
						return;
				 }
			else
				Main.searchReviewC.getReview();
		}
		
	}

	/**
	 * This method initializes jBackButton and set listener on this button	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJBackButton() {
		if (jBackButton == null) {
			jBackButton = new JButton();
			jBackButton.setBounds(new Rectangle(570, 60, 90, 30));
			jBackButton.setText("Back");
			jBackButton.addActionListener(this);
		}
		return jBackButton;
	}

	/**
	 * This method initializes jLogoutButton and set listener on this button
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJLogoutButton() {
		if (jLogoutButton == null) {
			jLogoutButton = new JButton();
			jLogoutButton.setBounds(new Rectangle(570, 20, 90, 30));
			jLogoutButton.setText("Logout");
			jLogoutButton.addActionListener(this);
		}
		return jLogoutButton;
	}

	/**
	 * This method initializes jSearchRList	
	 * 	
	 * @return javax.swing.JList	
	 */
	public JList getJSearchRList() {
		if (jSearchRList == null) {
			jSearchRList = new JList();
		}
		return jSearchRList;
	}
	/**
	 * This method initializes JScrollResAuthor	and set Portview on Author JList
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	public JScrollPane getJScrollResAuthor() {
		if (jScrollResAuthor == null) {
			jScrollResAuthor = new JScrollPane();
			jScrollResAuthor.setBounds(new Rectangle(555, 150, 145, 105));
			jScrollResAuthor.setViewportView(getJAuthorList());
		}
		return jScrollResAuthor;
	}
	/**
	 * This method initializes JAuthorList	
	 * 	
	 * @return javax.swing.JList	
	 */
	
	public JList getJAuthorList() {
		if (jAuthorList == null) {
			jAuthorList = new JList();
		}
		return jAuthorList;
	}

	/**
	 * This method initializes jShowRButton and set listener on this button	
	 * 	
	 * @return javax.swing.JButton	
	 */
	public JButton getJShowRButton() {
		if (jShowRButton == null) {
			jShowRButton = new JButton();
			jShowRButton.setBounds(new Rectangle(570, 285, 122, 34));
			jShowRButton.setText("Show Reviews");
			jShowRButton.addActionListener(this);
		}
		return jShowRButton;
	}
	/**
	 * This method initializes jScrollKeyWords and set Portview on Keywords TextField
	 * 	
	 * @return javax.swing.JScrollPane	
	 */
	public JScrollPane getJScrollKeyWords() {
		if (jScrollKeyWords == null) {
			jScrollKeyWords = new JScrollPane();
			jScrollKeyWords.setBounds(new Rectangle(80, 155, 145, 35));
			jScrollKeyWords.setViewportView(getJKeyWordsField());
		}
		return jScrollKeyWords;
	}

	/**
	 * This method initializes jKeyWordsField	
	 * 	
	 * @return javax.swing.JTextField	
	 */
	public JTextField getJKeyWordsField() {
		if (jKeyWordsField == null) {
			jKeyWordsField = new JTextField();
		}
		return jKeyWordsField;
	}

}
